package com.wanghua.mall.product.service.impl;

import com.wanghua.mall.product.entity.BrandEntity;
import com.wanghua.mall.product.entity.CategoryEntity;
import com.wanghua.mall.product.service.BrandService;
import com.wanghua.mall.product.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.Query;

import com.wanghua.mall.product.dao.BrandCategoryRelationDao;
import com.wanghua.mall.product.entity.BrandCategoryRelationEntity;
import com.wanghua.mall.product.service.BrandCategoryRelationService;

import javax.annotation.Resource;


@Service
public class BrandCategoryRelationServiceImpl extends ServiceImpl<BrandCategoryRelationDao, BrandCategoryRelationEntity> implements BrandCategoryRelationService {

    @Resource
    private CategoryService categoryService;
    @Resource
    private BrandService brandService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BrandCategoryRelationEntity> page = this.page(
                new Query<BrandCategoryRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 更新表中分类信息
     *
     * @param id   分类id
     * @param name 分类名称
     */
    @Override
    public void updateCategory(Long id, String name) {
        BrandCategoryRelationEntity relationEntity = new BrandCategoryRelationEntity();
        relationEntity.setCategoryName(name);
        QueryWrapper<BrandCategoryRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", id);
        baseMapper.update(relationEntity, queryWrapper);
    }

    /**
     * 更新表中品牌信息
     *
     * @param id   品牌id
     * @param name 品牌名称
     */
    @Override
    public void updateBrand(Long id, String name) {
        BrandCategoryRelationEntity relationEntity = new BrandCategoryRelationEntity();
        relationEntity.setBrandName(name);
        QueryWrapper<BrandCategoryRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id", id);
        baseMapper.update(relationEntity, queryWrapper);
    }

    /**
     * 根据品牌id获取品牌关联的分类
     *
     * @param brandId 品牌id
     * @return 品牌与分类关系列表
     */
    @Override
    public List<BrandCategoryRelationEntity> listByBrandId(Long brandId) {
        QueryWrapper<BrandCategoryRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id", brandId);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 新增品牌与分类关联关系,传递过来的参数是品牌id和分类id
     */
    @Override
    public void saveDetail(BrandCategoryRelationEntity brandCategoryRelation) {
        Long brandId = brandCategoryRelation.getBrandId();
        Long categoryId = brandCategoryRelation.getCategoryId();
        //保存品牌相关
        QueryWrapper<BrandEntity> brandEntityQueryWrapper = new QueryWrapper<>();
        brandEntityQueryWrapper.eq("id", brandId);
        brandEntityQueryWrapper.select("name");
        brandCategoryRelation.setBrandName(brandService.getOne(brandEntityQueryWrapper).getName());
        //保存分类相关
        QueryWrapper<CategoryEntity> categoryEntityQueryWrapper = new QueryWrapper<>();
        categoryEntityQueryWrapper.eq("id", categoryId);
        categoryEntityQueryWrapper.select("name");
        brandCategoryRelation.setCategoryName(categoryService.getOne(categoryEntityQueryWrapper).getName());
        //保存数据
        baseMapper.insert(brandCategoryRelation);
    }

    //根据分类id获取分类关联的品牌
    @Override
    public List<BrandCategoryRelationEntity> listByCategoryId(Long categoryId) {
        QueryWrapper<BrandCategoryRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        return baseMapper.selectList(queryWrapper);
    }

}